<template>

  <div>
    <div class="card" style="margin-bottom: 10px">
      <el-input style="width: 20vw; font-size:15px;" v-model="data.name" placeholder="请输入数据" prefix-icon="Search"/>
      <el-button type="primary" style="margin-left: 10px; " @click="load">搜索</el-button>
      <el-button type="warning" style="margin-left: 10px; " @click="reset">重置</el-button>
    </div>
    <div class="card" style="margin-bottom: 10px">
      <el-button type="primary" style="" @click="handleAdd">新增</el-button>
      <el-button type="danger" style="" @click="delBatch">批量删除</el-button>
<!--      <el-button type="success" style="">导入</el-button>-->
<!--      <el-button type="info" style="">导出</el-button>-->
    </div>
    <div class="card">
      <el-table :data="data.admin" stripe @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" />
        <el-table-column label="头像">
          <template #default="scope">
            <img v-if="scope.row.avatar" :src="scope.row.avatar" style="display:block; width: 35px; height: 35px; border-radius: 50%;"/>
          </template>
        </el-table-column>
        <el-table-column label="账号" prop="username"/>
        <el-table-column label="名称" prop="name"/>
        <el-table-column label="操作">
          <template #default="scope">
            <el-button type="primary" size="small" circle @click="handleUpdate(scope.row)">
              <el-icon><Edit /></el-icon>
            </el-button>
            <el-button type="danger" size="small" circle @click="del(scope.row.id)">
              <el-icon><Delete /></el-icon>
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <div style="margin-top:15px">
        <el-pagination
            @change="load"
            v-model:current-page="data.currentPage"
            v-model:page-size="data.pageSize"
            :page-sizes="[5, 10, 15, 20]"
            layout="total, sizes, prev, pager, next, jumper"
            :total="data.total"
        />
      </div>
    </div>

    <el-dialog v-model="data.formVisible" title="员工信息" width="500" destroy-on-close>
      <el-form ref="formRef" :rules="data.rules" :model="data.form" style="padding-right: 50px; padding-top:20px">
        <el-form-item label="头像" :label-width="80">
          <el-upload
              class="avatar-uploader"
              action="http://localhost:9090/files/upload"
              list-type="picture"
              :on-success="handleAvatarSuccess"
          >
            <el-button type="primary">上传头像</el-button>
          </el-upload>
        </el-form-item>

        <el-form-item label="账号" :label-width="80" prop="username">
          <el-input v-model="data.form.username" autocomplete="off" placeholder="请输入账号"/>
        </el-form-item>
        <el-form-item label="名称" :label-width="80" prop="name">
          <el-input v-model="data.form.name" autocomplete="off" placeholder="请输入名称"/>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="data.formVisible = false">取消</el-button>
          <el-button type="primary" @click="save">
            保存
          </el-button>
        </div>
      </template>
    </el-dialog>
  </div>


</template>

<script setup>

import {reactive, ref} from "vue";
import request from "@/utils/request.js";
import {ElMessage, ElMessageBox} from "element-plus";
import {Edit} from "@element-plus/icons-vue";

const data = reactive({
  name: null,
  currentPage: 1,
  pageSize: 5,
  total:0,
  admin: [],
  formVisible: false,
  form: {},
  ids: [],
  rules: {
    username: [
      {required: true, message: '请输入账号', trigger: 'blur'},
    ],
    name: [
      {required: true, message: '请输入名称', trigger: 'blur'},
    ]
  }
})

const handleAvatarSuccess = (res) => {
  data.form.avatar = res.data
}


const formRef = ref()

const load = () => {
  request.get('/admin/selectPage', {
    params: {
      pageNum: data.currentPage,
      pageSize: data.pageSize,
      name: data.name
    }
  }).then(res => {
    console.log(res)
    data.admin = res.data.list
    data.total = res.data.total
  })
}

const reset = () => {
  data.name = null
  load()
}

const handleAdd = () => {
  data.formVisible = true
  data.form = {}

}

const save = () => { //做两个操作 一个是新增，一个是标记
  formRef.value.validate((valid) => {
    if(valid){
      data.form.id ? update() : add()
    }
  })
}

const add = () => {
  request.post('/admin/add', data.form).then(res => { //没id
    if(res.code === '200'){
      ElMessage.success('保存成功');
      data.formVisible = false
      load()
    }else{
      ElMessage.error(res.msg);
    }
  })
}

const update = () => {
  request.put('/admin/updateById', data.form).then(res => { //有id
    if(res.code === '200'){
      ElMessage.success('保存成功');
      data.formVisible = false
      load()
    }else{
      ElMessage.error(res.msg);
    }
  })
}

const handleUpdate = (row) => {
  data.form = JSON.parse(JSON.stringify(row))
  data.formVisible = true
}

const del = (id) =>{
  ElMessageBox.confirm('确定要删除吗？', '删除确认', {type: 'warning'}).then(() =>{
    request.delete('/admin/deleteById/' +id).then(res => {
      if(res.code === '200'){
        ElMessage.success('删除成功');
        load()
      }else{
        ElMessage.error(res.msg);
      }
    })
  }).catch()
}

const handleSelectionChange = (rows) => { //返回所有选中的行
  console.log(rows)
  //从选中的行数组里面去除所有航的id组成一个新数组
  data.ids = rows.map(row => row.id)
}

const delBatch = () => {
  if(data.ids.length === 0){
    ElMessage.warning('请选择要删除的数据')
    return
  }
  ElMessageBox.confirm('确定要删除吗？', '删除确认',  {type: 'warning'}).then(() =>{
    request.delete('/admin/deleteBatch', {data: data.ids}).then(res => {
      if(res.code === '200'){
        ElMessage.success('删除成功');
        load()
      }else{
        ElMessage.error(res.msg);
      }
    })
  }).catch()
}

load()
</script>